package com.imut.onlinebanking.user.controller;

import com.imut.onlinebanking.base.response.JsonResult;
import com.imut.onlinebanking.base.response.StatusCode;
import com.imut.onlinebanking.user.pojo.dto.UserDTO;
import com.imut.onlinebanking.user.pojo.entity.User;
import com.imut.onlinebanking.user.pojo.vo.UserVO;
import com.imut.onlinebanking.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * 用户控制器:
 * 接收前端数据，调用业务逻辑层，返回结果给前端
 * 相当于：Servlet
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;


    @PostMapping("/login")
    public JsonResult login(@RequestBody UserDTO userDTO, HttpServletRequest request) {
        log.debug("login: " + userDTO);
        User user = new User();
        user.setUsername(userDTO.getUsername());
        user.setPassword(userDTO.getPassword());

        log.debug("selectUserByName: " + user);
        UserVO userVO = userService.selectUserByName(user);
        if (userVO == null) {
            return JsonResult.fail(StatusCode.USERNAME_ERROR);
        }

        // 获取 session 并设置属性
        request.getSession().setAttribute("currentUser", userVO);

        String redirectUrl;
        switch (userVO.getUserType()) {
            case "admin":
                redirectUrl = "/AdminHome.html";
                break;
            case "user":
                redirectUrl = "/UserHome.html";
                break;
            default:
                return new JsonResult(StatusCode.OPERATION_FAILED.getCode(), "用户类型错误");
        }

        return JsonResult.ok(redirectUrl);
    }

    @GetMapping("/currentUser")
    public JsonResult currentUser(HttpSession session) {
        UserVO currentUser = (UserVO) session.getAttribute("currentUser");
        System.out.println("currentUser: " + currentUser);
        log.debug("currentUser: " + currentUser);
        if (currentUser == null) {
            return JsonResult.fail(StatusCode.NOT_LOGIN);
        }
        return JsonResult.ok(currentUser);
    }
@PostMapping("/updateAddress")
public JsonResult updateAddress(@RequestBody UserDTO userDTO, HttpSession session) {
    System.out.println("userDTO = " + userDTO );



    UserVO currentUser = (UserVO) session.getAttribute("currentUser");
    System.out.println("currentUser = " + currentUser);
    if (currentUser == null) {
        return JsonResult.fail(StatusCode.NOT_LOGIN);
    }


    // 更新用户地址
    User user = new User();
    user.setUserId(currentUser.getUserId());
    user.setAddress(userDTO.getAddress());
    user.setPhoneNumber(userDTO.getPhoneNumber());
    user.setEmail(userDTO.getEmail());

    boolean success = userService.updateAddress(user);
    if (!success) {
        return JsonResult.fail(StatusCode.OPERATION_FAILED);
    }

    // 更新 Session 中的用户信息
    currentUser.setAddress(user.getAddress());
    currentUser.setPhoneNumber(user.getPhoneNumber());
    currentUser.setEmail(user.getEmail());
    session.setAttribute("currentUser", currentUser);

    return JsonResult.ok();
}
@PostMapping("/logout")
public JsonResult logout(HttpSession session) {
    UserVO currentUser = (UserVO) session.getAttribute("currentUser");
    if (currentUser == null) {
        return JsonResult.fail(StatusCode.NOT_LOGIN);
    }

    // 删除用户数据（可选）
    userService.deleteUserById(currentUser.getUserId());

    // 清除会话
    session.invalidate();

    return JsonResult.ok("注销成功");
}



}